import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Scanner;

/**
 * Created by Scott Tiger.
 * User: tomaszsocha
 * Date: 2010-03-29
 * Time: 13:02:21
 */
public class Objeto {
    private int wagaSuf;
    private HashMap<String,Object> laczenia= new HashMap<String,Object>();

    public HashMap<String, Integer> getWagi() {
        return wagi;
    }

    private HashMap <String,Integer> wagi = new HashMap<String,Integer>();

    public Objeto(String wagaPath, String laczeniaPath) throws FileNotFoundException {
        Scanner scanner = null;
        try {
            InputStream ms = getClass().getClassLoader().getResourceAsStream(wagaPath);
            scanner = new Scanner(ms,"UTF-8");
        } catch (Exception e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }



        try {
          //first use a Scanner to get each line
          while ( scanner.hasNextLine() ){
              String line = scanner.nextLine() ;
              String name = line.substring(0, line.indexOf(","));
              int waga = (int) Integer.parseInt(line.substring(line.indexOf(",")+1));
              wagi.put(name,waga);
          }
        }
        finally {
          //ensure the underlying stream is always closed
          scanner.close();
        }
        try {
            InputStream ms = getClass().getClassLoader().getResourceAsStream(laczeniaPath);
            scanner = new Scanner(ms,"UTF-8");
        } catch (Exception e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        try {
          //first use a Scanner to get each line
          while ( scanner.hasNextLine() ){
              String line = scanner.nextLine() ;
              String result = line.substring(0, line.indexOf(","));
              String param2 = line.substring(line.indexOf(",")+1,line.indexOf(",",line.indexOf(",")+1));
              String param1 = line.substring(line.indexOf(",",line.indexOf(",")+1)+1);
              HashMap<String,String> o = new HashMap<String,String>();
              if (laczenia.containsKey(param1)) {
              o = (HashMap<String,String>) laczenia.get(param1);}
              o.put(param2,result);
              laczenia.put(param1,o);
          }
        }
        finally {
          //ensure the underlying stream is always closed
          scanner.close();
        }
    }


    public String getPrefix() {
        return prefix;
    }

    public void setPrefix(String prefix) {
        this.prefix = prefix;
    }

    public String getSufix() {
        return sufix;
    }

    public void setSufix(String sufix) {
        this.sufix = sufix;
    }

    private String prefix;
    private String sufix;
    private int wagaPref;

    public int getWagaPref() {
        return wagaPref;
    }

    public void setWagaPref(int wagaPref) {
        this.wagaPref = wagaPref;
    }

    public int getWagaSuf() {
        return wagaSuf;
    }

    public void setWagaSuf(int wagaSuf) {
        this.wagaSuf = wagaSuf;
    }

    public String polacz(String pref1,String pref2){
       return (String) ((HashMap) laczenia.get(pref1)).get(pref2);

    }

    public Item polaczItemy(Item k1,Item k2){
         Item wynik;
         if ((k1.getSufix() == null && k1.getPrefix() == null ) ||  (k1.getSufix() == null && k1.getPrefix() == null )) {
         wynik = new  Item(polacz(k1.getName(),k2.getName()),k1,k2,wagi);
          } else {
         wynik = new  Item(polacz(k1.getPrefix(),k2.getPrefix()),polacz(k1.getName(),k2.getName()),polacz(k1.getSufix(),k2.getSufix()),k1,k2,wagi);
         }

         return wynik;
    }


}
